package com.jiyun.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.SpecificationsMapper;
import com.jiyun.pojo.Guig;
import com.jiyun.pojo.JsonRes;
import com.jiyun.pojo.Specifications;
import com.jiyun.service.GuigService;
import com.jiyun.service.SpecificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 刘清芳
 * @description: impl
 * @date: 2023/12/21 10:10
 */
@Service
public class SpecificationsServiceImpl extends ServiceImpl<SpecificationsMapper, Specifications> implements SpecificationsService {

    @Autowired
    SpecificationsService specificationService;
    @Autowired
    GuigService guigService;

    @Override
    public Page<Specifications> findAllspecifications(Specifications specifications, Integer pageNum, Integer pageSize) {

//        获取分页信息
        Page<Specifications> specificationsPage = new Page<>(pageNum, pageSize);
//        获取从表信息
        LambdaQueryWrapper<Guig> qw = new LambdaQueryWrapper<>();
//        根据主表的外键名称查
        // 根据规格名称模糊查询
        qw.like(StringUtils.isNotBlank(specifications.getGname()), Guig::getName, specifications.getGname());
        // 根据查询条件获取Guig列表
        List<Guig> list1 = guigService.list(qw);
        // 创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Specifications> wq = new LambdaQueryWrapper<>();
        // 创建整型列表
        ArrayList<Integer> integers = new ArrayList<>();
        // 遍历list1列表
        for (Guig guig : list1) {
            // 将id添加到整型列表中
            integers.add(guig.getId());
        }
        // 根据id进行in查询
        wq.in(Specifications::getGid, integers);
        // 根据规格分页查询
        Page<Specifications> page = specificationService.page(specificationsPage, wq);
        // 遍历page.getRecords()列表
        for (Specifications record : page.getRecords()) {
            // 遍历list1列表
            for (Guig guig : list1) {
                // 如果id相等
                if (record.getGid() == guig.getId()) {
                    // 设置规格名称
                    record.setGname(guig.getName());
                }
            }
        }
        // 返回规格分页信息
        return page;
    }

    //    添加和修改
    @Override
    public String findadd(Specifications specifications) {
// 创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Guig> guigLambdaQueryWrapper = new LambdaQueryWrapper<>();
// 根据名称等于条件查询Guig对象
        guigLambdaQueryWrapper.eq(Guig::getName, specifications.getGname());
// 获取满足查询条件的主表信息
        Guig one1 = guigService.getOne(guigLambdaQueryWrapper);
// 如果查询结果为空
        if (one1 == null) {
            // 创建新的Guig对象
            Guig guig = new Guig();
            // 设置名称属性为规格名称
            guig.setName(specifications.getGname());
            // 保存Guig对象
            guigService.save(guig);
            // 设置主表的id属性为Guig对象的id
            specifications.setGid(guig.getId());
            // 保存规格信息
            specificationService.save(specifications);
            return "直接添加成功！！！！！！！！！";
        }

//        创建一个条件集
        LambdaQueryWrapper<Specifications> wrapper = new LambdaQueryWrapper<>();
//        规格名称等值查询
        wrapper.like(Specifications::getSname, specifications.getSname());
//        获取查询结果
        Specifications one = specificationService.getOne(wrapper);
        int a = 0;
        Boolean b = false;
//        分割规格名称
        String[] split = one.getSname().split(",");
        for (int i = 0; i < split.length; i++) {
            if (!split[i].equals(specifications.getSname())) {
                one.setSname(one.getSname() + "," + specifications.getSname());
                b = true;
            } else if (split[i].equals(specifications.getSname())) {
                a = i;
                break;
            }
        }
//        分割规格属性
        if (b == true) {
            one.setAttribute(one.getAttribute() + ";" + specifications.getAttribute());
            specificationService.updateById(one);
            return "添加成功";
        }
        String[] split1 = one.getAttribute().split(";");
        String brand = "";
        for (int i = 0; i < split1.length; i++) {
            if (i == a) {
                String s = split1[a];
                split1[a] = s + "," + specifications.getAttribute() + ";";
            }
            brand = brand + split1[i] + ";";
        }
        // 设置one对象的属性为brand
        one.setAttribute(brand);
        // 更新specificationService中对应id的记录
        specificationService.updateById(one);

        return "添加成功";
    }

//    删除
    @Override
    public JsonRes del(Integer id) {
        specificationService.removeById(id);
        return JsonRes.success("删除成功");
    }
}
